<?php
class DBMPrimitiveTranslator implements DBMTranslator {
	/**
	 * Translate the primitive type to SQL Server notation
	 *
	 * @param mixed $attribute
	 */
	public static function translateToSQLServer($translatable, $command = null){
		return DBMPrimitiveTranslator::translate($translatable, DBManager::TYPE_MSSQL, $command);
	}
	
	/**
	 * Translate the primitive type to MySQL notation
	 *
	 * @param mixed $attribute
	 */
	public static function translateToMySQL($translatable, $command = null){
		return DBMPrimitiveTranslator::translate($translatable, DBManager::TYPE_MYSQL, $command);
	}
	
	/**
	 * Translate the primitive type to PostgreSQL notation
	 *
	 * @param mixed $attribute
	 */
	public static function translateToPostgreSQL($translatable, $command = null){
		return DBMPrimitiveTranslator::translate($translatable, DBManager::TYPE_POSTGRESQL, $command);
	}
	
	public static function translate($var, $connectionType, $command){
		if(is_null($var)){
			return "NULL";
		} elseif(is_string($var)){
			return DBMPrimitiveTranslator::translateString($var, $command);
		} elseif(is_bool($var)){
			return DBMPrimitiveTranslator::translateBoolean($var);
		} elseif(is_array($var)){
			return DBMPrimitiveTranslator::translateArray($var, $connectionType, $command); 
		} elseif(is_numeric($var)) {
			return $var;
		} else {
			return DBMPrimitiveTranslator::translateString($var, $command);
		}
	}
	public static function translateBoolean($bool){
		if($bool == true){
			return 1;
		} else {
			return 0;
		}
	}
	public static function translateArray($collection, $connectionType, DBMCommand $command){
		// search the array
		foreach($collection as $index => $var){
			if($connectionType == DBManager::TYPE_MSSQL){
				$collection[$index] = DBMGeneralTranslator::translateToSQLServer($var, $command);
			} elseif($connectionType == DBManager::TYPE_MYSQL){
				$collection[$index] = DBMGeneralTranslator::translateToMySQL($var, $command);
			} elseif($connectionType == DBManager::TYPE_POSTGRESQL){
				$collection[$index] = DBMGeneralTranslator::translateToPostgreSQL($var, $command);
			} else {
				throw new UnsupportedDatabaseException($connectionType);
			}
		}
		
		// return the imploded array
		return "( " . implode(", ", $collection) . " )";
	}
	public static function translateString($str, $command){
		if(is_null($command)){
			try{
				throw new Exception("blablabla");
			} catch(Exception $e){
				die($e->getTraceAsString());
			}
		}
		// check if the value is some column in the query
		foreach($command->getEntities() as $entity){
			// get the alias
			$alias = $entity->getAlias();
			
			// get the value with dot
			$value = $alias . ".";
			
			// check
			if(strpos($str, $value) !== false){
				return $str;
			}
		}
		
		// check if the value is some column in the join
		foreach($command->getJoins() as $join){
			// get the alias
			$alias = $join->getEntity()->getAlias();
			
			// get the value with dot
			$value = $alias . ".";
			
			// check
			if(strpos($str, $value) !== false){
				return $str;
			}
		}
		
		// check if the value is numeric
		if(is_numeric($str)){
			return $str;
		}
		
		// check the string length
		if(strlen($str) == 0){
			return "''";
		}
		
		// check if the value is an function
		if(strpos($str, " ") === false && @$str{strlen($str)-1} === ")"){
			return $str;
		}
		
		// check if the value isn't escaped
		if(@$str{0} != "'" || @$str{strlen($str)-1} != "'"){
			return sprintf("'%s'", $str);		
		} else {
			return $str;
		}
	}
}
?>